/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.bo.impl;

import who.tmap.bo.Annotations;
import who.tmap.bo.TextStyle;
import who.tmap.bo.BizObject;
import who.tmap.bo.BizObjects;
import who.tmap.bo.ThematicMap;
import who.tmap.common.TMapException;
import who.tmap.common.TMapFactory;

/**
 * A factory to instanciate domain objects
 * @author stl
 */
public class BizObjectFactory implements TMapFactory, BizObjects {

    private static BizObjectFactory instance =null;
    
    private static final String DEFAULT_TITLE ="Untitled";
    private static final String DEFAULT_FONT_NAME ="serif";
    private static final int DEFAULT_FONT_SIZE =10;
    private static final int DEFAULT_TITLE_FONT_SIZE =20;
    private static final int DEFAULT_LEGEND_FONT_SIZE =14;
    private static final String DEFAULT_LEGEND_TEXT ="Legend";    
    private static final int DEFAULT_FONT_STYLE =0;
    
    /**
     * Private constructor because it is a singleton
     *
     */
    private BizObjectFactory() {
    }
    
    /**
     * Lazy instanciation of the factory instance
     * @return
     */
    public static synchronized BizObjectFactory getInstance() {
        if ( instance == null ) {
            instance =new BizObjectFactory();
        }
        
        return instance;
    }
    
    /**
     * Instanciates business objects
     * @param bizObjectType
     * @return
     * @throws TMapException
     * @throws TMapException
     */
    public BizObject get( int bizObjectType ) throws TMapException {
        BizObject bizObject =null;
        
        switch( bizObjectType ) {
        	case ATTR_DEF:
        	    bizObject =new AttrDefImpl();
        	    break;
        	    
        	case ATTRS:
        	    bizObject =new AttrsImpl();
        	    break;
        	    
        	case THEMATIC_MAP:
        	    bizObject =new ThematicMapImpl();
        	    ((ThematicMap)bizObject).setAnnotations( (Annotations)get( ANNOTATIONS) );
        	    break;
        	    
        	case BASE_MAP:
        	    bizObject =new BaseMapImpl();
        	    break;   
        	    
        	case ANNOTATIONS:
        		bizObject =new AnnotationsImpl();
        		Annotations annotations =(Annotations)bizObject;
        		
        		annotations.setTitle( DEFAULT_TITLE );
        		
        		annotations.setLabelsVisible( false );
        		annotations.setTitleVisible( false ); 
        		annotations.setLegendVisible( false );
        		
        		annotations.setLabelsStyle( getNewTextStyle() );        		
        		annotations.setTitleStyle( getNewTextStyle() );
        		annotations.getTitleStyle().setFontSize( DEFAULT_TITLE_FONT_SIZE );
        		
        		annotations.setLegendStyle( getNewTextStyle() );
        		annotations.getLegendStyle().setFontSize( DEFAULT_LEGEND_FONT_SIZE );
        		annotations.setLegendText( DEFAULT_LEGEND_TEXT );
        		
        		break;
        		
        	case TEXT_STYLE:
        		bizObject =getNewTextStyle();//new TextStyleImpl();
        		break;     
        		
        	case MULTI_LAYER_THEMATIC_MAP:
        		bizObject =new MultiLayerThematicMapImpl();
        		break;
        	    
        	case THEMATIC_MAP_LAYER:
        		bizObject =new ThematicMapLayerImpl();
        		break;        		
        		
        	default:
        	    throw new TMapException( "Invalid business object type [" + bizObjectType + "]" );
        
        }
        
        return bizObject;
    }
    
    private TextStyle getNewTextStyle() {
		TextStyle style =new TextStyleImpl();
		
		style.setFontName( DEFAULT_FONT_NAME );
		style.setFontSize( DEFAULT_FONT_SIZE );
		style.setFontStyle( DEFAULT_FONT_STYLE ); 

		return style;
    }
}
